<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}信访分析系统{% endblock %}</title>
    <script src="/static/vendor/js/tailwindcss-3.4.17.js"></script>
    <!-- 本地Font Awesome样式 -->
    <!-- <link rel="stylesheet" href="/static/vendor/font-awesome/font-awesome.css"> -->
     <link rel="stylesheet" href="/static/vendor/css/font-awesome-6.4.0.all.min.css">
    <!-- 本地图表库 -->
    <script src="/static/vendor/chartjs/chart.min.js"></script>
    <script src="/static/vendor/echarts/echarts.min.js"></script>
    <script src="/static/vendor/leaflet/leaflet.js"></script>
    <link rel="stylesheet" href="/static/vendor/leaflet/leaflet.css">
    <!-- SheetJS for Excel file processing -->
    <script src="/static/js/xlsx-0.20.1-full.min.js"></script>
    <script src="/static/js/auth-manager.js"></script>
    <style>
        :root {
            --primary-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            --secondary-gradient: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            --success-gradient: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            --warning-gradient: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
        }

        * {
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: #f8fafc;
        }

        /* 自定义滚动条 */
        .custom-scrollbar::-webkit-scrollbar {
            width: 6px;
        }
        .custom-scrollbar::-webkit-scrollbar-track {
            background: #f1f5f9;
            border-radius: 3px;
        }
        .custom-scrollbar::-webkit-scrollbar-thumb {
            background: #cbd5e1;
            border-radius: 3px;
        }
        .custom-scrollbar::-webkit-scrollbar-thumb:hover {
            background: #94a3b8;
        }

        /* 侧边栏动画 */
        .sidebar {
            transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
        }
        .sidebar-collapsed {
            width: 64px !important;
        }
        .sidebar-expanded {
            width: 15% !important;
        }

        /* 文本淡入淡出 */
        .fade-text {
            transition: opacity 0.3s ease-in-out;
        }
        .fade-text.hidden {
            opacity: 0;
            pointer-events: none;
        }

        /* 统计卡片 */
        .stat-card {
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }
        .stat-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.4), transparent);
            transition: left 0.6s;
        }
        .stat-card:hover::before {
            left: 100%;
        }
        .stat-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 12px 30px rgba(0,0,0,0.15);
        }

        /* 玻璃态效果 */
        .glass-effect {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
        }

        /* 导航菜单项 */
        .nav-item {
            transition: all 0.3s ease;
            position: relative;
        }
        .nav-item::after {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 0;
            background: var(--primary-gradient);
            border-radius: 0 2px 2px 0;
            transition: height 0.3s ease;
        }
        .nav-item:hover::after,
        .nav-item.active::after {
            height: 70%;
        }
        .nav-item:hover {
            background: linear-gradient(90deg, rgba(102, 126, 234, 0.1) 0%, transparent 100%);
        }
        .nav-item.active {
            background: linear-gradient(90deg, rgba(102, 126, 234, 0.15) 0%, transparent 100%);
            color: #667eea;
        }

        /* 图表容器 */
        .chart-container {
            position: relative;
            height: 300px;
        }

        /* 最新活动项 */
        .activity-item {
            transition: all 0.3s ease;
        }
        .activity-item:hover {
            transform: translateX(5px);
            background: rgba(102, 126, 234, 0.05);
        }

        /* 用户下拉菜单 */
        .user-dropdown {
            transform-origin: top right;
            transition: all 0.2s ease;
        }
        .user-dropdown.hidden {
            opacity: 0;
            transform: scale(0.95);
            pointer-events: none;
        }
        .user-dropdown.show {
            opacity: 1;
            transform: scale(1);
            pointer-events: all;
        }
    </style>
    {% block extra_css %}{% endblock %}
</head>
<body class="bg-slate-50">
    <div class="flex h-screen overflow-hidden">
        <!-- 左侧边栏 -->
        <div id="sidebar" class="sidebar sidebar-expanded bg-white shadow-xl border-r border-slate-200 flex flex-col">
            <!-- 应用标题 -->
            <div class="p-6 border-b border-slate-200">
                <div class="flex items-center justify-between">
                    <div class="flex items-center space-x-3">
                        <div class="w-10 h-10 rounded-xl bg-gradient-to-br from-indigo-500 to-purple-600 flex items-center justify-center shadow-lg">
                            <i class="fas fa-file-contract text-white text-lg"></i>
                        </div>
                        <div class="fade-text">
                            <h1 class="font-bold text-xl bg-gradient-to-r from-indigo-600 to-purple-600 bg-clip-text text-transparent">{{ app_name }}</h1>
                            <p class="text-xs text-slate-500">智能分析平台</p>
                        </div>
                    </div>
                    <button id="sidebarToggle" class="p-2 rounded-lg hover:bg-slate-100 transition-colors">
                        <i class="fas fa-bars text-slate-600"></i>
                    </button>
                </div>
            </div>

            <!-- 导航菜单 -->
            <div class="flex-1 overflow-y-auto custom-scrollbar">
                <nav class="p-4">
                    <ul class="space-y-2">
                          {% if current_user and (current_user.is_superuser or current_user.has_permission('analysis_result_view')) %}
                        <li>
                            <a href="/web/analysis-results" class="nav-item {% if request.url.path == '/web/analysis-results' %}active{% endif %} flex items-center space-x-3 px-4 py-3 rounded-lg cursor-pointer" onclick="return handleNavigation('/web/analysis-results')">
                                <i class="fas fa-search text-lg"></i>
                                <span class="fade-text font-medium">分析结果</span>
                            </a>
                        </li>
                        {% endif %}

                        <!-- 信访台账菜单 -->
                        {% if current_user and (current_user.is_superuser or current_user.has_permission('analysis_result_view')) %}
                        <li>
                            <a href="/web/petition-ledger" class="nav-item {% if request.url.path == '/web/petition-ledger' %}active{% endif %} flex items-center space-x-3 px-4 py-3 rounded-lg cursor-pointer" onclick="return handleNavigation('/web/petition-ledger')">
                                <i class="fas fa-clipboard-list text-lg"></i>
                                <span class="fade-text font-medium">信访台账</span>
                            </a>
                        </li>
                        {% endif %}


                        {% if current_user and (current_user.is_superuser or current_user.has_permission('admin_access') or current_user.has_permission('statistics_view')) %}
                        <li>
                            <a href="/web/regional-analysis" class="nav-item {% if request.url.path == '/web/regional-analysis' %}active{% endif %} flex items-center space-x-3 px-4 py-3 rounded-lg cursor-pointer" onclick="return handleNavigation('/web/regional-analysis')">
                                <i class="fas fa-map text-lg"></i>
                                <span class="fade-text font-medium">区域分析</span>
                            </a>
                        </li>
                        {% endif %}
                        
                        <!-- AI分析配置菜单 -->
                        {% if current_user and (current_user.is_superuser or current_user.has_permission('admin_access') or current_user.has_permission('statistics_view')) %}
                        <li>
                            <a href="/web/complaint-analysis" class="nav-item {% if request.url.path == '/web/complaint-analysis' %}active{% endif %} flex items-center space-x-3 px-4 py-3 rounded-lg cursor-pointer" onclick="return handleNavigation('/web/complaint-analysis')">
                                <i class="fas fa-map-marker-alt text-lg"></i>
                                <span class="fade-text font-medium">投诉类型分析</span>
                            </a>
                        </li>
                        {% endif %}
                        <!-- 新增分析菜单项 -->
                        {% if current_user and (current_user.is_superuser or current_user.has_permission('admin_access') or current_user.has_permission('analysis_result_view')) %}
                        <li>
                            <a href="/web/sentiment-analysis" class="nav-item {% if request.url.path == '/web/sentiment-analysis' %}active{% endif %} flex items-center space-x-3 px-4 py-3 rounded-lg cursor-pointer" onclick="return handleNavigation('/web/sentiment-analysis')">
                                <i class="fas fa-map-marked-alt text-lg"></i>
                                <span class="fade-text font-medium">情感分析</span>
                            </a>
                        </li>
                        {% endif %}

                        {% if current_user and (current_user.is_superuser or current_user.has_permission('admin_access') or current_user.has_permission('analysis_result_view')) %}
                        <li>
                            <a href="/web/compliance-analysis" class="nav-item {% if request.url.path == '/web/compliance-analysis' %}active{% endif %} flex items-center space-x-3 px-4 py-3 rounded-lg cursor-pointer" onclick="return handleNavigation('/web/compliance-analysis')">
                                <i class="fas fa-tags text-lg"></i>
                                <span class="fade-text font-medium">规范性分析</span>
                            </a>
                        </li>
                        {% endif %}

                        {% if current_user and (current_user.is_superuser or current_user.has_permission('admin_access') or current_user.has_permission('analysis_result_view')) %}
                        <li>
                            <a href="/web/duplicate-analysis" class="nav-item {% if request.url.path == '/web/duplicate-analysis' %}active{% endif %} flex items-center space-x-3 px-4 py-3 rounded-lg cursor-pointer" onclick="return handleNavigation('/web/duplicate-analysis')">
                                <i class="fas fa-heart text-lg"></i>
                                <span class="fade-text font-medium">重复分析</span>
                            </a>
                        </li>
                        {% endif %}
                        <!-- 隐藏的报告生成菜单项
                        {% if current_user and (current_user.is_superuser or current_user.has_permission('admin_access') or current_user.has_permission('analysis_result_view')) %}
                        <li>
                            <a href="/web/compliance-analysis" class="nav-item {% if request.url.path == '/web/compliance-analysis' %}active{% endif %} flex items-center space-x-3 px-4 py-3 rounded-lg cursor-pointer" onclick="return handleNavigation('/web/compliance-analysis')">
                                <i class="fas fa-check-circle text-lg"></i>
                                <span class="fade-text font-medium">规范性分析</span>
                            </a>
                        </li>
                        {% endif %}
                        {% if current_user and (current_user.is_superuser or current_user.has_permission('report_view')) %}
                        <li>
                            <a href="/web/report-generation" class="nav-item {% if request.url.path == '/web/report-generation' %}active{% endif %} flex items-center space-x-3 px-4 py-3 rounded-lg cursor-pointer" onclick="return handleNavigation('/web/report-generation')">
                                <i class="fas fa-file-alt text-lg"></i>
                                <span class="fade-text font-medium">生成报告</span>
                            </a>
                        </li>
                        <li>
                            <a href="/web/ai-reports" class="nav-item {% if request.url.path == '/web/ai-reports' %}active{% endif %} flex items-center space-x-3 px-4 py-3 rounded-lg cursor-pointer" onclick="return handleNavigation('/web/ai-reports')">
                                <div class="relative">
                                    <i class="fas fa-robot text-lg"></i>
                                    <span class="absolute -top-1 -right-1 bg-green-500 text-white text-xs rounded-full w-3 h-3 flex items-center justify-center">AI</span>
                                </div>
                                <span class="fade-text font-medium">智能报告</span>
                            </a>
                        </li>
                        {% endif %} -->
                        
                        <!-- 管理员功能 -->
                        {% if current_user and (current_user.is_superuser or current_user.has_permission('admin_access') or current_user.has_permission('system_config') or current_user.has_permission('analysis_task_execute')) %}
                        <li class="pt-4 mt-4 border-t border-slate-200">
                            <p class="fade-text text-xs text-slate-500 font-semibold px-4 mb-2">管理员功能</p>
                        </li>
                        {% endif %}
                        
                        {% if current_user and (current_user.is_superuser or current_user.has_permission('admin_access') or current_user.has_permission('analysis_task_execute')) %}
                        <li>
                            <a href="/web/ai-analysis" class="nav-item {% if request.url.path == '/web/ai-analysis' %}active{% endif %} flex items-center space-x-3 px-4 py-3 rounded-lg cursor-pointer" onclick="return handleNavigation('/web/ai-analysis')">
                                <i class="fas fa-robot text-lg"></i>
                                <span class="fade-text font-medium">AI分析功能</span>
                            </a>
                        </li>
                        {% endif %}
                        
                        {% if current_user and (current_user.is_superuser or current_user.has_permission('admin_access') or current_user.has_permission('system_config')) %}
                        <li>
                            <a href="/web/system-settings" class="nav-item {% if request.url.path == '/web/system-settings' %}active{% endif %} flex items-center space-x-3 px-4 py-3 rounded-lg cursor-pointer" onclick="return handleNavigation('/web/system-settings')">
                                <i class="fas fa-cog text-lg"></i>
                                <span class="fade-text font-medium">系统设置</span>
                            </a>
                        </li>
                        <!-- <li>
                            <a href="/web/security" class="nav-item {% if request.url.path == '/web/security' %}active{% endif %} flex items-center space-x-3 px-4 py-3 rounded-lg cursor-pointer" onclick="return handleNavigation('/web/security')">
                                <div class="relative">
                                    <i class="fas fa-shield-alt text-lg"></i>
                                    <span class="absolute -top-1 -right-1 bg-red-500 text-white text-xs rounded-full w-3 h-3 flex items-center justify-center">!</span>
                                </div>
                                <span class="fade-text font-medium">安全管理</span>
                            </a>
                        </li> -->
                        {% endif %}
                        
                        {% if current_user and current_user.is_superuser %}
                        <li>
                            <a href="/web/account-management" class="nav-item {% if request.url.path == '/web/account-management' %}active{% endif %} flex items-center space-x-3 px-4 py-3 rounded-lg cursor-pointer" onclick="return handleNavigation('/web/account-management')">
                                <div class="relative">
                                    <i class="fas fa-users-cog text-lg"></i>
                                    <span class="absolute -top-1 -right-1 bg-blue-500 text-white text-xs rounded-full w-3 h-3 flex items-center justify-center">新</span>
                                </div>
                                <span class="fade-text font-medium">账号管理</span>
                            </a>
                        </li>
                        {% endif %}
                    </ul>
                </nav>
            </div>

            <!-- 用户信息 -->
            {% if current_user %}
            <div class="p-4 border-t border-slate-200">
                <div class="flex items-center space-x-3">
                    <div class="w-12 h-12 rounded-full bg-gradient-to-br from-emerald-400 to-cyan-500 flex items-center justify-center shadow-lg">
                        <i class="fas fa-user text-white text-lg"></i>
                    </div>
                    <div class="fade-text flex-1">
                        <p class="font-semibold text-slate-800">{{ current_user.full_name or current_user.username }}</p>
                        <p class="text-xs text-slate-500">{{ current_user.email }}</p>
                        {% if current_user.role_names %}
                        <p class="text-xs text-slate-400">{{ current_user.role_names|join(', ') }}</p>
                        {% endif %}
                    </div>
                    <div class="relative">
                        <button id="userMenuBtn" class="fade-text p-2 rounded-lg hover:bg-slate-100 transition-colors">
                            <i class="fas fa-chevron-down text-slate-600"></i>
                        </button>
                        <!-- 下拉菜单 -->
                        <div id="userMenu" class="user-dropdown hidden absolute right-0 bottom-full mb-2 w-48 bg-white rounded-lg shadow-lg border border-slate-200">
                            <a href="#" class="block px-4 py-2 text-sm text-slate-700 hover:bg-slate-100 rounded-t-lg">
                                <i class="fas fa-user-circle mr-2"></i>个人中心
                            </a>
                            <a href="#" class="block px-4 py-2 text-sm text-slate-700 hover:bg-slate-100">
                                <i class="fas fa-cog mr-2"></i>账户设置
                            </a>
                            <hr class="my-1 border-slate-200">
                            <a href="/web/logout" class="block px-4 py-2 text-sm text-slate-700 hover:bg-slate-100 rounded-b-lg">
                                <i class="fas fa-sign-out-alt mr-2"></i>退出登录
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            {% endif %}
        </div>

        <!-- 主内容区 -->
        <div class="flex-1 flex flex-col overflow-hidden">
            <!-- 顶部栏 -->
            <header class="bg-white border-b border-slate-200 px-6 py-4">
                <div class="flex items-center justify-between">
                    <div>
                        <h2 class="text-2xl font-bold text-slate-800">{% block page_title %}{% endblock %}</h2>
                        <p class="text-sm text-slate-500">{% block page_subtitle %}{% endblock %}</p>
                    </div>
                    <div class="flex items-center space-x-4">
                        <!-- 通知图标 -->
                        <button class="relative p-2 rounded-lg hover:bg-slate-100 transition-colors">
                            <i class="fas fa-bell text-slate-600"></i>
                            <span class="absolute top-1 right-1 w-2 h-2 bg-red-500 rounded-full"></span>
                        </button>
                        <!-- 全屏按钮 -->
                        <button id="fullscreenBtn" class="p-2 rounded-lg hover:bg-slate-100 transition-colors">
                            <i class="fas fa-expand text-slate-600"></i>
                        </button>
                        <!-- 时间显示 -->
                        <div class="text-sm text-slate-600">
                            <i class="far fa-clock mr-1"></i>
                            <span id="currentTime"></span>
                        </div>
                    </div>
                </div>
            </header>

            <!-- 滚动内容区 -->
            <main class="flex-1 overflow-y-auto custom-scrollbar p-6">
                {% block content %}{% endblock %}
            </main>
        </div>
    </div>
    <script>
        // 侧边栏切换
        document.getElementById('sidebarToggle')?.addEventListener('click', function() {
            const sidebar = document.getElementById('sidebar');
            const fadeTexts = document.querySelectorAll('.fade-text');
            
            if (sidebar.classList.contains('sidebar-expanded')) {
                sidebar.classList.remove('sidebar-expanded');
                sidebar.classList.add('sidebar-collapsed');
                fadeTexts.forEach(text => text.classList.add('hidden'));
            } else {
                sidebar.classList.remove('sidebar-collapsed');
                sidebar.classList.add('sidebar-expanded');
                setTimeout(() => {
                    fadeTexts.forEach(text => text.classList.remove('hidden'));
                }, 200);
            }
        });

        // AJAX请求封装
        async function apiRequest(url, options = {}) {
            const defaultOptions = {
                headers: {
                    'Content-Type': 'application/json',
                }
            };

            // 如果有token，添加到请求头（优先从localStorage获取，其次从cookie获取，最后从URL参数获取）
            let token = localStorage.getItem('auth_token');
            if (!token) {
                // 从cookie获取token
                const cookieToken = document.cookie
                    .split('; ')
                    .find(row => row.startsWith('auth_token='));
                if (cookieToken) {
                    token = cookieToken.split('=')[1];
                }
            }
            if (!token) {
                token = new URLSearchParams(window.location.search).get('token');
            }
            if (token) {
                defaultOptions.headers['Authorization'] = `Bearer ${token}`;
            }

            const finalOptions = {
                ...defaultOptions,
                ...options,
                headers: {
                    ...defaultOptions.headers,
                    ...options.headers
                }
            };

            // 如果提供了body且不是字符串，转换为JSON字符串
            if (finalOptions.body && typeof finalOptions.body !== 'string') {
                finalOptions.body = JSON.stringify(finalOptions.body);
            }

            try {
                const response = await fetch(url, finalOptions);
                const data = await response.json();

                if (!response.ok) {
                    throw new Error(data.detail || data.message || '请求失败');
                }

                return data;
            } catch (error) {
                console.error('API请求失败:', error);
                // 确保错误信息能够正确显示
                let errorMessage = '请求失败';
                if (error.message) {
                    errorMessage = error.message;
                } else if (typeof error === 'object') {
                    errorMessage = JSON.stringify(error);
                } else {
                    errorMessage = String(error);
                }
                throw new Error(errorMessage);
            }
        }

        // 显示通知
        // function showNotification(message, type = 'success') {
        //     // 移除已存在的通知
        //     const existingNotifications = document.querySelectorAll('.notification');
        //     existingNotifications.forEach(n => n.remove());

        //     const notification = document.createElement('div');
        //     notification.className = `notification ${type}`;
        //     notification.textContent = message;

        //     // 确保添加到body的末尾，避免影响页面结构
        //     document.body.appendChild(notification);

        //     // 3秒后移除
        //     setTimeout(() => {
        //         if (notification.parentNode) {
        //             notification.remove();
        //         }
        //     }, 3000);
        // }

        // 格式化文件大小
        function formatFileSize(bytes) {
            if (bytes === 0) return '0 Bytes';
            const k = 1024;
            const sizes = ['Bytes', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));
            return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
        }

        // 格式化日期
        function formatDate(dateString) {
            const date = new Date(dateString);
            return date.toLocaleString('zh-CN');
        }

        // 处理页面导航
        function handleNavigation(url) {
            // 使用auth-manager.js处理导航
            return window.authManager.handleNavigation(url);
        }

        // 安全的页面跳转函数
        function navigateTo(url) {
            window.authManager.navigateTo(url);
        }
    </script>
    <script>
<!--        // 侧边栏切换-->
<!--        document.getElementById('sidebarToggle').addEventListener('click', function() {-->
<!--            const sidebar = document.getElementById('sidebar');-->
<!--            const fadeTexts = document.querySelectorAll('.fade-text');-->
<!--            -->
<!--            if (sidebar.classList.contains('sidebar-expanded')) {-->
<!--                sidebar.classList.remove('sidebar-expanded');-->
<!--                sidebar.classList.add('sidebar-collapsed');-->
<!--                fadeTexts.forEach(text => text.classList.add('hidden'));-->
<!--            } else {-->
<!--                sidebar.classList.remove('sidebar-collapsed');-->
<!--                sidebar.classList.add('sidebar-expanded');-->
<!--                setTimeout(() => {-->
<!--                    fadeTexts.forEach(text => text.classList.remove('hidden'));-->
<!--                }, 200);-->
<!--            }-->
<!--        });-->

        // 用户菜单切换
        const userMenuBtn = document.getElementById('userMenuBtn');
        if (userMenuBtn) {
            userMenuBtn.addEventListener('click', function(e) {
                e.stopPropagation();
                const userMenu = document.getElementById('userMenu');
                if (userMenu.classList.contains('hidden')) {
                    userMenu.classList.remove('hidden');
                    userMenu.classList.add('show');
                } else {
                    userMenu.classList.add('hidden');
                    userMenu.classList.remove('show');
                }
            });
        }

        // 点击其他地方关闭用户菜单
        document.addEventListener('click', function() {
            const userMenu = document.getElementById('userMenu');
            if (userMenu && !userMenu.classList.contains('hidden')) {
                userMenu.classList.add('hidden');
                userMenu.classList.remove('show');
            }
        });

        // 全屏功能
        const fullscreenBtn = document.getElementById('fullscreenBtn');
        if (fullscreenBtn) {
            fullscreenBtn.addEventListener('click', function() {
                if (!document.fullscreenElement) {
                    document.documentElement.requestFullscreen();
                    this.innerHTML = '<i class="fas fa-compress text-slate-600"></i>';
                } else {
                    document.exitFullscreen();
                    this.innerHTML = '<i class="fas fa-expand text-slate-600"></i>';
                }
            });
        }

        // 时间显示
        function updateTime() {
            const now = new Date();
            const timeString = now.toLocaleString('zh-CN', {
                year: 'numeric',
                month: '2-digit',
                day: '2-digit',
                hour: '2-digit',
                minute: '2-digit',
                second: '2-digit'
            });
            document.getElementById('currentTime').textContent = timeString;
        }
        
        updateTime();
        // 只在显示时间元素的页面上启动定时器
        const timeElement = document.getElementById('currentTime');
        if (timeElement) {
            window.timeUpdateInterval = setInterval(updateTime, 1000);
            
            // 页面隐藏时停止定时器，节省资源
            document.addEventListener('visibilitychange', function() {
                if (document.hidden) {
                    clearInterval(window.timeUpdateInterval);
                } else {
                    updateTime(); // 立即更新时间
                    window.timeUpdateInterval = setInterval(updateTime, 1000);
                }
            });
        }

        // 检查登录状态
        document.addEventListener('DOMContentLoaded', function() {
            // 如果是登录页面，不进行登录状态检查
            if (window.location.pathname.includes('/login')) {
                return;
            }
            
            // 页面加载时自动检查token有效性
            if (window.authManager.isLoggedIn()) {
                window.authManager.validateToken().then(isValid => {
                    if (!isValid) {
                        console.log('Token已失效，请重新登录');
                        window.authManager.navigateTo('/web/login');
                    }
                });
            } else {
                // 如果没有登录且不是登录页面，则跳转到登录页
                window.authManager.navigateTo('/web/login');
            }
        });
    </script>
    
    {% block extra_js %}{% endblock %}
</body>
</html>